/*
 * @Author: cqg
 * @Date: 2020-11-12 20:32:14
 * @LastEditors: cqg
 * @LastEditTime: 2020-12-28 11:08:36
 * @Description: file content
 */
import dataList from "@/components/dataList";
import accounting from "@/views/wy-finance/bill/views/accounting/components/accounting.vue";
import contractView from "@/views/rent/contract/view.vue";
import {
	getContractList,
	getAccountList,
	submit,
	abolition
} from "@/views/wy-finance/bill/api/accounting";
export default {
	components: {
		dataList,
		contractView,
		accounting
	},
	provide() {
		return {
			// 更新核算列表
			initList: this.getAccountList,
			// 更新合同列表
			updateList: this.getContractList
		};
	},
	beforeMount() {
		this.getContractList();
	},
	data() {
		return {
			Flag: true, // 第一次传递状态
			selectOp: [
				{
					label: "核查状态",
					name: "checkStatus",
					option: [
						{ label: "全部", value: "" },
						{ label: "本次所有", value: "01" },
						{ label: "未核算", value: "02" },
						{ label: "已核算", value: "03" }
					]
				},
				{
					label: "合同类别",
					name: "category",
					option: this.getDict("contract_category")
				}
			],
			// 合同列表请求参数
			contract: {
				// 列表数据
				tableData: [],
				// 请求参数
				params: {
					current: 1,
					size: 10,
					propertyNames: "",
					checkStatus: ""
				},
				// 分页
				pager: {
					total: 0, // 总页数
					current: 1, // 当前页数
					size: 10 // 每页显示多少条
				},
				// 页面loading
				loading: false
			},
			// 核算列表请求参数
			account: {
				// 筛选列表
				filterList: [
					//   { label: '收款项目', inputType: 'dict', dictName: "charge_project_type", name: 'chargepropertyNames', multiple: true },
					{
						label: "费用周期",
						inputType: "daterange",
						name: ["costPeriodStartDate", "costPeriodEndDate"]
					},
					{
						label: "账单日期",
						inputType: "daterange",
						name: ["billDateStart", "billDateEnd"]
					},
					{
						label: "结算状态",
						inputType: "dict",
						dictName: "lessor_bill_status",
						name: "billStatus",
						multiple: true,
						defaultValue: ["00"]
					}
				],
				// 表格列表数据
				tableData: [],
				// 请求参数
				params: {
					current: 1,
					size: 10,
					propertyNames: "",
					billStatus: "00"
				},
				// 选中的数据
				selectDataArr: [],
				// 分页
				pager: {
					total: 0, // 总页数
					current: 1, // 当前页数
					size: 10 // 每页显示多少条
				},
				// 页面loading
				loading: true
			}
		};
	},
	methods: {
		submit(row) {
			this.$confirm("此操作将提交该账单, 是否继续?", "提示", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				type: "warning"
			})
				.then(() => {
					submit(row.billId).then(() => {
						this.changeAccountList(row.rentContractId);
					});
					this.$message({
						type: "success",
						message: "提交成功!"
					});
				})
				.catch(() => {
					this.$message({
						type: "info",
						message: "已取消提交"
					});
				});
		},
		abolition(row) {
			this.$confirm("此操作将作废该账单, 是否继续?", "提示", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				type: "warning"
			})
				.then(() => {
					abolition(row.billId).then(() => {
						this.changeAccountList(row.rentContractId);
						this.$message.success("作废成功!");
					});
				})
				.catch(() => {
					this.$message({
						type: "info",
						message: "已取消作废"
					});
				});
		},
		// 切换核算列表
		changeAccountList(rentContractId) {
			this.contract.tableData = this.contract.tableData.map(item => ({
				...item,
				active: item.rentContractId == rentContractId
			}));
			this.Flag = true;
			this.$refs.dataList.setFilter(new Map([["billStatus", ["00"]]]));
			this.getAccountList({ rentContractId, current: 1, billStatus: "00" });
		},
		// 获取合同列表
		getContractList(params = {}) {
			this.contract.loading = true;
			let requestData = { ...this.contract.params, ...params };
			getContractList(requestData)
				.then(res => {
					const { current, size, total, records } = res.data.data;
					this.contract.tableData = records.map((item, index) => ({
						...item,
						active: index === 0
					}));
					this.contract.params = requestData;
					this.contract.pager = { total, current, size };
					// 回调获取核算列表
					records.length > 0
						? this.getAccountList({
								rentContractId: records[0].rentContractId,
								billStatus: ["00"]
						  })
						: (this.account.loading = false);
				})
				.catch(() => {
					this.account.loading = false;
				})
				.finally(() => {
					this.contract.loading = false;
				});
		},
		// 获取核算列表
		getAccountList(params = {}) {
			this.account.loading = true;
			let req = { ...this.account.params, ...params };
			if (!req.rentContractId) return;
			getAccountList(req)
				.then(res => {
					const { current, size, total, records } = res.data.data;
					this.account.tableData = records;
					this.account.params = req;
					this.account.pager = { total, current, size };
				})
				.finally(() => {
					this.account.loading = false;
				});
		},
		// 合同状态字典值格式化
		dictVal_bill_status(row, column, cellValue) {
			let obj = this.getDict("lessor_bill_status").find(
				p => p.value == cellValue
			);
			if (obj) {
				return obj.label;
			}
			return "";
		}
	}
};
